

using System;

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_CopySign_RT : RT_Node
    {
        public RT_SingleValuePort Magnitude_RT;

        public RT_SingleValuePort Sign_RT;

        public RT_SingleValuePort Result_RT;

        public Node_CopySign_RT(Node_CopySign sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            Magnitude_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Magnitude, this);

            Sign_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Sign, this);

            Result_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Result, this);
            Result_RT.OnGetValue = GetResult;
        }

        public AValue GetResult(DAG_Executor executor)
        {
            var valueType = Result_RT.SourceValue.ValueType;

            if (valueType == AValue.EValueType.Integer)
            {
                int magnitude = Magnitude_RT.GetValue(executor).ToInteger().Item2;
                int sign = Sign_RT.GetValue(executor).ToInteger().Item2;
                int result;
                if (sign == 0)
                {
                    result = magnitude;
                }
                else
                {
                    result = (int)MathF.Abs(magnitude) * (sign > 0 ? 1 : -1);
                }
                Result_RT.SourceValue.SetInteger(result);
            }
            else if (valueType == AValue.EValueType.Scalar)
            {
                float magnitude = Magnitude_RT.GetValue(executor).ToScalar().Item2;
                float sign = Sign_RT.GetValue(executor).ToScalar().Item2;
                float result;
                if (sign == 0)
                {
                    result = magnitude;
                }
                else
                {
                    result = MathF.Abs(magnitude) * (sign > 0 ? 1f : -1f);
                }
                Result_RT.SourceValue.SetScalar(result);
            }

            return Result_RT.SourceValue;
        }
    }
}